package com.ajie.service.impl;

import com.ajie.entity.Course;
import com.ajie.entity.New;
import com.ajie.service.LookLikeFavouriteService;
import com.ajie.service.CourseService;
import com.ajie.service.MomentService;
import com.ajie.service.NewService;
import com.ajie.utils.*;
import com.ajie.vo.MomentVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class LookLikeFavouriteServiceImpl implements LookLikeFavouriteService {
    
    
    @Autowired
    private MomentService momentService;
    
    @Autowired
    private NewService newService;
    
    @Autowired
    private CourseService courseService;
    
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    JedisUtils jedisUtils ;
    
    @Override
    public Result findPageByNewMomentCourse(QueryInfo queryInfo) {
        log.info("该文件名是：LookLikeFavouriteServiceImpl,该方法是：findPageByNewMomentCourse");
        log.info("1.开始新闻咨询数据分页-->页码{}, -->页数{}, -->查询内容{}",queryInfo.getPageNumber(),queryInfo.getPageSize(),queryInfo.getQueryString());
        String[] split = queryInfo.getQueryString().split("::");
        String type = split[0];
        String favouriteOrLook = split[1];
        List<MomentVo> momentList = momentService.getAll();
        List<New> newList = newService.getAll();
        List<Course> courseList = courseService.getAll();
        List<New> newList1 = new ArrayList<>(newList.size());
        List<Course> courseList1 = new ArrayList<>(courseList.size());
        List<MomentVo> momentVoList = new ArrayList<>(momentList.size());
        if ("moment".equals(type)){
            momentList.forEach(momentVo->{
                //收藏点赞或浏览的新闻
                Set<Object> set = redisUtil.sGet("momentId::"+ momentVo.getId().toString());
                //如果非空，遍、
                for (Object item2 : set){
                    String[] split1 = item2.toString().split("::");
                    String uid = split1[2];
                    //我点赞收藏或浏览的新闻
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if (favouriteOrLook.equals(split1[0])){
                            momentVoList.add(momentVo);
                        }
                    }
                }
            });
            //需求列表
            momentVoList.forEach(momentVo->{
                //这个新闻对应的所有人的浏览点赞收藏
                Set<Object> set = redisUtil.sGet("momentId::"+ momentVo.getId().toString());
                Long lookNum = jedisUtils.zscore("looks","moment::"+momentVo.getId()).longValue();
                Long likeNum = jedisUtils.zscore("likes","moment::"+momentVo.getId()).longValue();
                Long favouriteNum = jedisUtils.zscore("favourites","moment::"+momentVo.getId()).longValue();
                momentVo.setLookNum(lookNum);
                momentVo.setLikeNum(likeNum);
                momentVo.setFavouriteNum(favouriteNum);
                boolean b = false;
                boolean b1 = false;
                boolean b2 = false;
                for (Object item2 : set){
                    String[] split1 = item2.toString().split("::");
                    String uid = split1[2];
                    String time = split1[4];
                    //这个新闻对应的我的浏览或点赞或收藏
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if ("look".equals(split1[0])){
                            b = true;
                            momentVo.setLookTime(time);
                        }
                        if ("like".equals(split1[0])){
                            b1 = true;
                            momentVo.setLikeTime(time);
                        }
                        if ("favourite".equals(split1[0])){
                            b2 = true;
                            momentVo.setFavouriteTime(time);
                        }
                    }
                }
                momentVo.setLook(b);
                momentVo.setLike(b1);
                momentVo.setFavourite(b2);
            });
        }
        if ("new".equals(type)){
            //所有新闻
            newList.forEach(new1->{
                //收藏点赞或浏览的新闻
                Set<Object> set = redisUtil.sGet("newId::"+ new1.getId().toString());
                //如果非空，遍、
                for (Object item2 : set){
                    String[] split1 = item2.toString().split("::");
                    String uid = split1[2];
                    //我点赞收藏或浏览的新闻
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if (favouriteOrLook.equals(split1[0])){
                            newList1.add(new1);
                        }
                    }
                }
            });
            //需求列表
            newList1.forEach(new1->{
                //这个新闻对应的所有人的浏览点赞收藏
                Set<Object> set = redisUtil.sGet("newId::"+ new1.getId().toString());
                Long lookNum = jedisUtils.zscore("looks","new::"+new1.getId()).longValue();
                Long likeNum = jedisUtils.zscore("likes","new::"+new1.getId()).longValue();
                Long favouriteNum = jedisUtils.zscore("favourites","new::"+new1.getId()).longValue();
                new1.setLookNum(lookNum);
                new1.setLikeNum(likeNum);
                new1.setFavouriteNum(favouriteNum);
                boolean b = false;
                boolean b1 = false;
                boolean b2 = false;
                for (Object item2 : set){
                    String[] split1 = item2.toString().split("::");
                    String uid = split1[2];
                    String time = split1[4];
                    //这个新闻对应的我的浏览或点赞或收藏
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if ("look".equals(split1[0])){
                            b = true;
                            new1.setLookTime(time);
                        }
                        if ("like".equals(split1[0])){
                            b1 = true;
                            new1.setLikeTime(time);
                        }
                        if ("favourite".equals(split1[0])){
                            b2 = true;
                            new1.setFavouriteTime(time);
                        }
                    }
                }
                new1.setLook(b);
                new1.setLike(b1);
                new1.setFavourite(b2);
            });
            
        }
        if ("course".equals(type)){
            courseList.forEach(course->{
                //收藏点赞或浏览的新闻
                Set<Object> set = redisUtil.sGet("courseId::"+ course.getId().toString());
                //如果非空，遍、
                for (Object item2 : set){
                    String[] split1 = item2.toString().split("::");
                    String uid = split1[2];
                    //我点赞收藏或浏览的新闻
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if (favouriteOrLook.equals(split1[0])){
                            courseList1.add(course);
                        }
                    }
                }
            });
            courseList1.forEach(course->{
                //这个新闻对应的所有人的浏览点赞收藏
                Set<Object> set = redisUtil.sGet("courseId::"+ course.getId().toString());
                Long lookNum = jedisUtils.zscore("looks","course::"+course.getId()).longValue();
                Long likeNum = jedisUtils.zscore("likes","course::"+course.getId()).longValue();
                Long favouriteNum = jedisUtils.zscore("favourites","course::"+course.getId()).longValue();
                course.setLookNum(lookNum);
                course.setLikeNum(likeNum);
                course.setFavouriteNum(favouriteNum);
                boolean b = false;
                boolean b1 = false;
                boolean b2 = false;
                for (Object item2 : set){
                    String[] split1 = item2.toString().split("::");
                    String uid = split1[2];
                    String time = split1[4];
                    //这个新闻对应的我的浏览或点赞或收藏
                    if (SecurityUtil.getUserId().toString().equals(uid)){
                        if ("look".equals(split1[0])){
                            b = true;
                            course.setLookTime(time);
                        }
                        if ("like".equals(split1[0])){
                            b1 = true;
                            course.setLikeTime(time);
                        }
                        if ("favourite".equals(split1[0])){
                            b2 = true;
                            course.setFavouriteTime(time);
                        }
                    }
                }
                course.setLook(b);
                course.setLike(b1);
                course.setFavourite(b2);
            });
        }
        
        //4.把list进行分页
        Page page = new Page(queryInfo.getPageNumber(),queryInfo.getPageSize());
        // 获取list的大小
        int total = 0;
        if ("moment".equals(type)){
            total = momentVoList.size();
            if ("look".equals(favouriteOrLook)){
                momentVoList.sort(Comparator.comparing(MomentVo::getLookTime).reversed());
            }
            if ("favourite".equals(favouriteOrLook)){
                log.info("moment{}",momentVoList);
                momentVoList.sort(Comparator.comparing(MomentVo::getFavouriteTime).reversed());
            }
        }
        // 获取list的大小
        if ("new".equals(type)){
            total = newList1.size();
            if ("look".equals(favouriteOrLook)){
                newList1.sort(Comparator.comparing(New::getLookTime).reversed());
            }
            if ("favourite".equals(favouriteOrLook)){
                newList1.sort(Comparator.comparing(New::getFavouriteTime).reversed());
            }
        }
        // 获取list的大小
        if ("course".equals(type)){
            total = courseList1.size();
            if ("look".equals(favouriteOrLook)){
                courseList1.sort(Comparator.comparing(Course::getLookTime).reversed());
            }
            if ("favourite".equals(favouriteOrLook)){
                courseList1.sort(Comparator.comparing(Course::getFavouriteTime).reversed());
            }
        }
    
        page.setTotal(total);
        int startIndex = (queryInfo.getPageNumber()-1) * queryInfo.getPageSize();
        int endIndex = Math.min(startIndex+queryInfo.getPageSize(),total);
        if (startIndex< total){
            if ("moment".equals(type))  page.addAll(momentVoList.subList(startIndex,endIndex));
            if ("new".equals(type)) page.addAll(newList1.subList(startIndex,endIndex));
            if ("course".equals(type)) page.addAll(courseList1.subList(startIndex,endIndex));
        }
        // objectPageInfo就是分好页的对象
        PageInfo<Object> objectPageInfo = new PageInfo<>(page);
        //5.传给前端
        return new PageResult(total,objectPageInfo.getList());
    }
}
